from django.contrib.auth.models import User
from models import SirogiUser
from django.conf import settings
from django.contrib.auth.backends import ModelBackend

class SirogiUserModelBackend(ModelBackend):
    def authenticate(self, username=None, password=None):
        try:
            user = SirogiUser.objects.get(username=username)
            if user.check_password(password):
                return user
        except SirogiUser.DoesNotExist:
            try:
                user = User.objects.get(username=username)
                if user.check_password(password):
                    return user
            except User.DoesNotExist:
                return None

    def get_user(self, user_id):
        try:
            return SirogiUser.objects.get(pk=user_id)
        except SirogiUser.DoesNotExist:
            try:
                return User.objects.get(pk=user_id)
            except:
                return None
